package com.happle.gcmclient.managers;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.happle.DAL.DBHelper;
import com.happle.gcmclient.config.Constants;
import com.happle.gcmclient.obj.LngObj;

/**
 * Created by Inna on 26.10.13.
 */
public class LngManager {

	private com.happle.DAL.DBHelper DBHelper;

	// Creates the helper, the DB connection
	public LngManager(Context context) {
		DBHelper = new DBHelper(context);
	}

	public ArrayList<LngObj> getAllLanguages() {
		ArrayList<LngObj> lstLanguages = new ArrayList<LngObj>();
		SQLiteDatabase db = DBHelper.getReadableDatabase();
		Cursor convCursor = db.rawQuery("SELECT * FROM "
				+ Constants.TBL_LANGUAGES + "", null);

		// Runs over the lines in the table
		while (convCursor.moveToNext()) {
			// Takes the columns, saves each one into a parameter
			String _lng_id = convCursor.getString(0);
			String _lng_name = convCursor.getString(1);
			boolean _is_marked = (convCursor.getInt(2) == 1) ? true : false;
			boolean _is_sent = (convCursor.getInt(3) == 1) ? true : false;
			LngObj newLngObj = new LngObj(_lng_id, _lng_name, _is_marked,
					_is_sent);
			lstLanguages.add(newLngObj);
		}
		convCursor.close();
		db.close();
		return (lstLanguages);
	}

	/**
	 * Get all language recorder which not synced to server database
	 * 
	 * @return
	 */
	public ArrayList<LngObj> getAllLanguagesToSync() {
		ArrayList<LngObj> lstLanguages = new ArrayList<LngObj>();
		SQLiteDatabase db = DBHelper.getReadableDatabase();
		Cursor convCursor = db.rawQuery("SELECT * FROM "
				+ Constants.TBL_LANGUAGES + " where " + Constants.is_marked_p
				+ "== '1' AND "+Constants.IS_SENT +" == '0'", null);
        if (convCursor.getCount() > 0) {
            convCursor = db.rawQuery("SELECT * FROM "
                    + Constants.TBL_LANGUAGES + " where " + Constants.is_marked_p
                    + "== '1' ", null);
		while (convCursor.moveToNext()) {
			// Takes the columns, saves each one into a parameter
			String _lng_id = convCursor.getString(0);
			String _lng_name = convCursor.getString(1);
			boolean _is_marked = (convCursor.getInt(2) == 1) ? true : false;
			boolean _is_sent = false;
			LngObj newLngObj = new LngObj(_lng_id, _lng_name, _is_marked,
					_is_sent);
			lstLanguages.add(newLngObj);
		}
        }
		convCursor.close();
		db.close();
		return (lstLanguages);
	}

	public ArrayList<LngObj> getSelectedLanguages(boolean selected) {
		ArrayList<LngObj> lstLanguages = new ArrayList<LngObj>();
		try {
			SQLiteDatabase db = DBHelper.getReadableDatabase();
			int selInt = 0;
			if (selected) {
				selInt = 1;
			}
			Cursor convCursor = db.rawQuery("SELECT * FROM "
					+ Constants.TBL_LANGUAGES + " WHERE is_marked = " + selInt,
					null);
			while (convCursor.moveToNext()) {
				// Takes the columns, saves each one into a parameter
				String _lng_id = convCursor.getString(0);
				String _lng_name = convCursor.getString(1);
				boolean _is_marked = (convCursor.getInt(2) == 1) ? true : false;
				boolean _is_sent = (convCursor.getInt(3) == 1) ? true : false;
				LngObj newLngObj = new LngObj(_lng_id, _lng_name, _is_marked,
						_is_sent);
				lstLanguages.add(newLngObj);
			}
			convCursor.close();
			db.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return (lstLanguages);
	}

	public ArrayList<LngObj> getLanguage(String lng_id) {
		ArrayList<LngObj> lstLanguages = new ArrayList<LngObj>();
		SQLiteDatabase db = DBHelper.getReadableDatabase();
		Cursor convCursor = db.rawQuery("SELECT * FROM "
				+ Constants.TBL_LANGUAGES + " WHERE " + Constants.lng_id_p
				+ "  = '" + lng_id + "'", null);

		// Runs over the lines in the table
		while (convCursor.moveToNext()) {
			// Takes the columns, saves each one into a parameter
			String _lng_id = convCursor.getString(0);
			String _lng_name = convCursor.getString(1);
			boolean _is_marked = (convCursor.getInt(2) == 1) ? true : false;
			boolean _is_sent = (convCursor.getInt(3) == 1) ? true : false;
			LngObj newLngObj = new LngObj(_lng_id, _lng_name, _is_marked,
					_is_sent);
			lstLanguages.add(newLngObj);
		}
		convCursor.close();
		db.close();
		return (lstLanguages);
	}

	// Update Message
	public boolean updateLngSettings(LngObj newLngObj) {
		boolean result = false;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		try {
			ContentValues values = new ContentValues();

			values.put(Constants.lng_name_p, newLngObj.get_lng_name());
			values.put(Constants.is_marked_p,
					(newLngObj.is_is_marked() == true) ? 1 : 0);
			values.put(Constants.is_sent_p,
					(newLngObj.is_is_sent() == true) ? 1 : 0);
			int res = db.update(Constants.TBL_LANGUAGES, values,
					Constants.lng_id_p + "='" + newLngObj.get_lng_id() + "'",
					null);
			if (res >= 0) {
				result = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		db.close();
		return result;
	}

	// Update Message's Status
	public boolean updateLngStatus(LngObj newLngObj) {
		boolean result = false;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		try {
			ContentValues values = new ContentValues();

			values.put(Constants.is_marked_p,
					(newLngObj.is_is_marked() == true) ? 1 : 0);
			values.put(Constants.is_sent_p, (newLngObj.is_is_sent() ? 1 : 0));
			int res = db.update(Constants.TBL_LANGUAGES, values,
					Constants.lng_id_p + "='" + newLngObj.get_lng_id() + "'",
					null);
			if (res >= 0) {
				result = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		db.close();
		return result;
	}

	public LngObj addLanguage(LngObj lngObj) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		// Put values by column name
		values.put(Constants.lng_id_p, lngObj.get_lng_id());
		values.put(Constants.lng_name_p, lngObj.get_lng_name());
		values.put(Constants.is_marked_p, (lngObj.is_is_marked() ? 1 : 0));
		values.put(Constants.is_sent_p, (lngObj.is_is_sent() ? 1 : 0));
		long insertId = db.insert(Constants.TBL_LANGUAGES, null, values);
		Log.d("Insert language", "Return value : " + insertId);
		// Close the connection with the DB
		db.close();
		return lngObj;
	}

	public boolean addListLanguage(ArrayList<LngObj> listLngObj) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		ContentValues values;
		for (LngObj lngObj : listLngObj) {
			values = new ContentValues();
			// Put values by column name
			values.put(Constants.lng_id_p, lngObj.get_lng_id());
			values.put(Constants.lng_name_p, lngObj.get_lng_name());
			values.put(Constants.is_marked_p, (lngObj.is_is_marked() ? 1 : 0));
			values.put(Constants.is_sent_p, (lngObj.is_is_sent() ? 1 : 0));
			long insertId = db.insert(Constants.TBL_LANGUAGES, null, values);
			Log.d("Insert language", "Return value : " + insertId);
		}
		// Close the connection with the DB
		db.close();
		return true;
	}
}
